package guoyuan.szkingdom.android.module_home.homeui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.alibaba.android.arouter.launcher.ARouter
import com.gy.depend.lib_common.base.mvvm.vm.EmptyViewModel
import com.gy.depend.lib_common.base.ui.BaseActivity
import com.gy.depend.lib_common.base.ui.BaseFragmentStateAdapter
import dagger.hilt.android.AndroidEntryPoint
import guoyuan.szkingdom.android.module_home.databinding.HomeActivityInternalLayoutBinding
import guoyuan.szkingdom.android.module_home.fragment.InternalFragment
import kotlin.random.Random

/**
 * - 文件描述 : ViewPager2+fragment 模拟Fragment页面重建。
 */
@AndroidEntryPoint
class InternalPagerActivity : BaseActivity<HomeActivityInternalLayoutBinding, EmptyViewModel>() {

    override val mViewModel: EmptyViewModel by viewModels()

    private val mCreateFragmentData = mutableListOf<String>()

    private var mAdapter: InternalPagerFragmentAdapter? = null


    override fun HomeActivityInternalLayoutBinding.initView() {
        addFragment.setOnClickListener {
            //添加一个随机页面
            mAdapter?.addData("Pager ID:${Random.nextInt()}")
            // 1. 应用内简单的跳转(通过URL跳转在'进阶用法'中)
            ARouter.getInstance().build("/test/activity").navigation()
        }
        removeFragment.setOnClickListener {
            //移除当前展示页面
//            mAdapter?.removeData("更多")
            mAdapter?.removeData(viewPager.currentItem)

            ARouter.getInstance().build("/test/main_activity").navigation()
        }
        initPager()
    }

    private fun initPager() {
        mCreateFragmentData.add("首页")
        mCreateFragmentData.add("我的")
        mCreateFragmentData.add("设置")
        mCreateFragmentData.add("更多")
        mCreateFragmentData.add("动态")
        mAdapter = InternalPagerFragmentAdapter(this, mCreateFragmentData)
        mBinding.viewPager.adapter = mAdapter

    }

    override fun initObserve() {}

    override fun initRequestData() {}

    class InternalPagerFragmentAdapter(activity: FragmentActivity, data: MutableList<String> = mutableListOf()) :
        BaseFragmentStateAdapter<String>(activity, data) {
        override fun createFragment(item: String, position: Int): Fragment {
            val bundle = Bundle().apply {
                putString("What", item)
            }
            return when (item) {
                "首页" -> {
                    //假装首页
                    InternalFragment()
                }
                "我的" -> {
                    //假装我的
                    InternalFragment()
                }
                "设置" -> {
                    //假装设置
                    InternalFragment()
                }
                "更多" -> {
                    //假装更多
                    InternalFragment()
                }
                else -> {
                    //另外动态item创建类型
                    InternalFragment()
                }
            }.apply {
                //设置传递参数bundle
                arguments = bundle
            }
        }


    }
}